////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                                                                            //
//                                                         WNProject                                                          //
//                                                                                                                            //
//            This file is distributed under the BSD 2-Clause open source license. See License.txt for details.               //
//                                                                                                                            //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#pragma once

#ifndef __WN_CONCURRENCY_READ_WRITE_MUTEX_H__
#define __WN_CONCURRENCY_READ_WRITE_MUTEX_H__

#include "WNCore/inc/WNTypes.h"

#ifdef _WN_WINDOWS
    #include <windows.h>
#elif defined _WN_LINUX
    #include <pthread.h>
#endif

namespace WNConcurrency {
    class WNReadWriteMutex {
    public:
        WN_FORCE_INLINE WNReadWriteMutex();
        WN_FORCE_INLINE ~WNReadWriteMutex();

        WN_FORCE_INLINE WN_VOID LockReader();
        WN_FORCE_INLINE WN_BOOL TryLockReader();
        WN_FORCE_INLINE WN_VOID UnlockReader();

        WN_FORCE_INLINE WN_VOID LockWriter();
        WN_FORCE_INLINE WN_BOOL TryLockWriter();
        WN_FORCE_INLINE WN_VOID UnlockWriter();

    private:
        WNReadWriteMutex(const WNReadWriteMutex&);
        WNReadWriteMutex& operator = (const WNReadWriteMutex&);

    private:
        #ifdef _WN_WINDOWS
            SRWLOCK mSlimReadWriteLock;
        #elif defined _WN_LINUX
            pthread_rwlock_t mReadWriteLock;
        #endif
    };
}

#include "WNConcurrency/inc/WNReadWriteMutex.inl"

#endif // __WN_CONCURRENCY_READ_WRITE_MUTEX_H__